/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.controllers;

import com.insurance.cis.scos.CampaignSearchCondition;
import com.insurance.cis.utils.CisConstants;
import com.insurance.cis.utils.MessageDialogUtils;
import com.insurance.cis.utils.Utils;
import com.insurance.cis.views.CampaignMasterJPanelView;
import com.insurance.cis.views.EstimatedCampaignJPanelView;
import java.text.ParseException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JPanel;

/**
 *
 * @author TruongLQ
 */
public class CampaignMasterController {

    final Pattern pattern = Pattern.compile(CisConstants.DATE_PATTERN_COMPILE);
    JPanel parentJpnl;

    public CampaignMasterController(JPanel jPanel) {
        this.parentJpnl = jPanel;
    }

    public CampaignSearchCondition validateSearchConditions() {
        String channelIDStr = CisConstants.BLANK;
        String startDateStr = CisConstants.BLANK;
        String endDateStr = CisConstants.BLANK;
        String campaignID = CisConstants.BLANK;
        String campaignName = CisConstants.BLANK;
        String productID = CisConstants.BLANK;
        if (parentJpnl instanceof CampaignMasterJPanelView) {
            CampaignMasterJPanelView parent = (CampaignMasterJPanelView) parentJpnl;
            channelIDStr = parent.getTxtChannelID().getText();
            startDateStr = parent.getTxtStartDate().getText();
            endDateStr = parent.getTxtEndDate().getText();
            campaignID = parent.getTxtCampaignID().getText();
            campaignName = parent.getTxtCampaignName().getText();
            productID = parent.getTxtProductID().getText();
        } else if (parentJpnl instanceof EstimatedCampaignJPanelView) {
            EstimatedCampaignJPanelView parent = (EstimatedCampaignJPanelView) parentJpnl;
            channelIDStr = parent.getTxtChannelID().getText();
            startDateStr = parent.getTxtStartDate().getText();
            endDateStr = parent.getTxtEndDate().getText();
            campaignID = parent.getTxtCampaignID().getText();
            campaignName = parent.getTxtCampaignName().getText();
            productID = parent.getTxtProductID().getText();
        }

        Integer channelID = null;
        if (!channelIDStr.isEmpty()) {
            try {
                channelID = Integer.parseInt(channelIDStr.trim());
            } catch (NumberFormatException ex) {
                if (parentJpnl instanceof CampaignMasterJPanelView) {
                    ((CampaignMasterJPanelView) parentJpnl).getTxtChannelID().requestFocus();
                } else if (parentJpnl instanceof EstimatedCampaignJPanelView) {
                    ((EstimatedCampaignJPanelView) parentJpnl).getTxtChannelID().requestFocus();
                }
                MessageDialogUtils.dialogError(parentJpnl, "Channel ID must be numeric!");
                return null;
            }
        }
        Date startDate;
        try {
            startDate = validateDate(startDateStr);
        } catch (ParseException ex) {
            if (parentJpnl instanceof CampaignMasterJPanelView) {
                ((CampaignMasterJPanelView) parentJpnl).getTxtStartDate().requestFocus();
            } else if (parentJpnl instanceof EstimatedCampaignJPanelView) {
                ((EstimatedCampaignJPanelView) parentJpnl).getTxtStartDate().requestFocus();
            }
            MessageDialogUtils.dialogError(parentJpnl, "Start date must be in format dd/mm/yyyy!");
            return null;
        }
        Date endDate;
        try {
            endDate = validateDate(endDateStr);
        } catch (ParseException ex) {
            if (parentJpnl instanceof CampaignMasterJPanelView) {
                ((CampaignMasterJPanelView) parentJpnl).getTxtEndDate().requestFocus();
            } else if (parentJpnl instanceof EstimatedCampaignJPanelView) {
                ((EstimatedCampaignJPanelView) parentJpnl).getTxtEndDate().requestFocus();
            }
            MessageDialogUtils.dialogError(parentJpnl, "End date must be in format dd/mm/yyyy!");
            return null;
        }

        if (startDate != null && endDate != null && startDate.after(endDate)) {
            MessageDialogUtils.dialogError(parentJpnl, "Start date must be <= end date!");
            if (parentJpnl instanceof CampaignMasterJPanelView) {
                ((CampaignMasterJPanelView) parentJpnl).getTxtStartDate().requestFocus();
            } else if (parentJpnl instanceof EstimatedCampaignJPanelView) {
                ((EstimatedCampaignJPanelView) parentJpnl).getTxtStartDate().requestFocus();
            }
            return null;
        }

        return new CampaignSearchCondition(campaignID.trim(), campaignName.trim(), 
                productID.trim(), channelID, startDate, endDate);
    }

    private Date validateDate(String date) throws ParseException {
        Date dateRet = null;

        if (!date.isEmpty()) {
            if (date.trim().length() != 10) {
                throw new ParseException("Date parse exception", 0);
            }
            Matcher matcher = pattern.matcher(date.trim());
            if (!matcher.matches()) {
                throw new ParseException("Date parse exception", 0);
            }
            dateRet = Utils.parseDate(date.trim(),
                    CisConstants.DATE_DD_MM_YYYY);
        }
        return dateRet;
    }
}
